<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/WEB-INF/common/taglibs.jsp" %>
<div class="main-content">
    <div class="breadcrumbs" id="breadcrumbs">
        <div class="col-md-8 col-xs-12">
            <%@include file="/WEB-INF/common/back/breadcrumb.jsp" %>
        </div>
        <div class="col-md-4 text-right">
            <c:if test='${fn:contains(qClassActions,"add")}'>
                <world:hasPermission permissions="/sys/role/add">
                    <a class="btn btn-primary btn-sm no-border" href="${contextPath}/sys/role/add">
                        <i class="ace-icon fa fa-plus"></i>添加
                    </a>
                </world:hasPermission>
            </c:if>
            <c:if test='${fn:contains(qClassActions,"export")}'>
                <a class="btn btn-primary btn-sm no-border" href="${contextPath}/sys/role/export">
                    <i class="ace-icon fa fa-plus"></i>导出
                </a>
            </c:if>
        </div>
    </div>

    <div class="page-content">
        <div class="row">
            <div class="col-md-12">
                <form class="form-inline" id="searchForm" method="post">
                    <input name="pageIndex" type="hidden">
                    <%@include file="/WEB-INF/common/back/fragment/query-fragement.jsp" %>
                    <button type="button" id="queryBtn" class="btn btn-purple btn-sm no-border">
                        <i class="ace-icon fa fa-search fa-on-right"></i>查询
                    </button>
                    <div class="space-4"></div>
                </form>
            </div>
            <div class="col-xs-12">
                <div id="jqxgrid"></div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        var source = {
            datatype: 'json',
            datafields: [
                {name: 'id', type: 'string'},
                {name: 'roleName', type: 'string'},
                {name: 'creator', type: 'string'},
                {name: 'createTimeStr', type: 'string'},
                {name: '操作', type: 'string'}
            ],
            id: 'id',
            url: '${contextPath}/sys/role/list',
            type: 'POST',
            async: true,
            filter: function () {
                $("#jqxgrid").jqxGrid('updatebounddata', 'filter');
            },
            beforeprocessing: function (data) {
                if (data != null) {
                    source.totalrecords = data.totalElements;
                }
            }
        };

        var jqxgrid = $('#jqxgrid');
        var dataAdapter = new $.jqx.dataAdapter(source, {
            formatData: function (data) {
                $.extend(data, {
                    params: JSON.stringify($("#searchForm").serializeObject())
                });
                return data;
            }
        });
        jqxgrid.jqxGrid({
            theme: 'bootstrap',
            width: '100%',
            autoheight: true,
            source: dataAdapter,
            pageable: true,
            filterable: false,
            enablebrowserselection: true,
            columnsresize: true,
            pagesize: 10,
            pagesizeoptions: ['10', '15', '30'],
            virtualmode: true,
            altrows: true,
            rendergridrows: function (obj) {
                return obj.data;
            },
            columns: [
                {text: '角色名称', datafield: 'roleName'},
                {text: '创建人', datafield: 'creator'},
                {text: '创建时间', datafield: 'createTimeStr'},
                {text: '操作', datafield: '操作', width: '33%', columntype: 'string', cellsrenderer: handleRender}
            ]
        });


        jqxgrid.on('bindingcomplete', function (event) {
            $("#jqxgrid").jqxGrid('localizestrings', localizationobj);
        });

        $("#queryBtn").click(function () {
            $('#jqxgrid').jqxGrid('updatebounddata');
        });
    });

    function del(id) {
        layer.confirm('确定删除？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                type: "POST",
                url: "${contextPath}/sys/role/del",
                dataType: "json",
                data: {
                    ids: id
                },
                success: function (json) {
                    if (json.success) {
                        $('#jqxgrid').jqxGrid('updatebounddata');
                        layer.closeAll('dialog');
                    } else {
                        layer.msg(json.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg("对不起，系统异常！");
                }
            });
        }, function () {

        });
    }

    function handleRender(row, column, value) {
        console.log(row);
        var data = $('#jqxgrid').jqxGrid('getrowdata', row);
        if (data == undefined) {
            return "";
        }
        var btn = "&nbsp";
        <%--btn += "<a class='btn btn-xs btn-success' href='${contextPath}/sys/role/view?id=" + data.id + "'><i class='ace-icon fa fa-search-plus bigger-120'></i></a> ";--%>
        <c:if test = '${fn:contains(qClassActions,"edit")}' >
        <world:hasPermission permissions="/sys/role/edit">
        btn += "<a class='btn btn-xs btn-info' href='${contextPath}/sys/role/edit?id=" + data.id + "'>编辑</a> ";
        </world:hasPermission>
        </c:if>
        <c:if test = '${fn:contains(qClassActions,"del")}' >
        <world:hasPermission permissions="/sys/role/del">
        btn += '<button class="btn btn-xs btn-danger" onclick="del(\'' + data.id + '\')">删除</button> ';
        </world:hasPermission>
        </c:if>
        <world:hasPermission permissions="/sys/role/menu">
        btn += "<a class='btn btn-xs btn-yellow' href='${contextPath}/sys/role/menu?id=" + data.id + "'>配置菜单</a> ";
        </world:hasPermission>
        <world:hasPermission permissions="/sys/role/resource">
        btn += "<a class='btn btn-xs btn-success' href='${contextPath}/sys/role/resource?id=" + data.id + "'>配置权限</a> ";
        </world:hasPermission>
        return btn;
    }
</script>